import pygame
import os

import setting as code


class JumpBool:
    def __init__(self):
        os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (200, 100)
        pygame.init()
        pygame.display.set_caption("ssss")
        self.screen = pygame.display.set_mode((code.window_w, code.window_h), pygame.DOUBLEBUF, 32)
        self.clock = pygame.time.Clock()

        # self.vx

    def position(self, x, y, vx, vy):
        """
        计算小球当前的位置,速度
        :return: (x,y)
        """

        # 计算小球下一刻的速度，位置
        vy += round(code.g * 1 / code.fps, 0)
        x += vx * 1 / code.fps
        y += vy * 1 / code.fps
        # 小球碰到地面则反向
        if y >= code.window_h - 10:
            vy = -vy
        return (x, y, vx, vy)

    def start(self):
        x, y = code.window_w / 2, 10  # 球的坐标
        vx, vy = 0, 0  # 在x,y轴上的速度
        # 游戏主循环
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    # 游戏退出
                    exit()
            x, y, vx, vy = self.position(x, y, vx, vy)
            self.screen.fill((0, 0, 0))
            pygame.draw.circle(self.screen, (255, 0, 0), (int(x), int(y)), 10)
            pygame.display.update()
            time_passed = self.clock.tick(code.fps)
